package edu.uvic.TabViz.framework;

import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.content.SharedPreferences;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import edu.uvic.TabViz.TabVizActivity;


public class Renderer implements GLSurfaceView.Renderer {

	protected TabVizActivity mContext;

	protected boolean pause = false;
	
	protected int mFrameRate = 0;

	protected SharedPreferences preferences;

	protected int mViewportWidth, mViewportHeight;
	protected GLSurfaceView mSurfaceView;
	protected Timer mTimer;

	protected float[] mVMatrix = new float[16];

	protected boolean mEnableDepthBuffer = true;

	protected boolean mClearChildren = false;
	
	
	public Renderer(TabVizActivity context) {
		mContext = context;
	}

	public void onDrawFrame(GL10 glUnused) {
		if (pause) return;
	}

	public boolean isPause() {
		return pause;
	}

	public void setPause(boolean pause) {
		this.pause = pause;
	}

	public TabVizActivity getmContext() {
		return mContext;
	}

	public void setmContext(TabVizActivity mContext) {
		this.mContext = mContext;
	}

	public int getmFrameRate() {
		return mFrameRate;
	}

	public void setmFrameRate(int mFrameRate) {
		this.mFrameRate = mFrameRate;
	}

	public SharedPreferences getPreferences() {
		return preferences;
	}

	public void setPreferences(SharedPreferences preferences) {
		this.preferences = preferences;
	}

	public int getmViewportWidth() {
		return mViewportWidth;
	}

	public void setmViewportWidth(int mViewportWidth) {
		this.mViewportWidth = mViewportWidth;
	}

	public int getmViewportHeight() {
		return mViewportHeight;
	}

	public void setmViewportHeight(int mViewportHeight) {
		this.mViewportHeight = mViewportHeight;
	}

	public GLSurfaceView getmSurfaceView() {
		return mSurfaceView;
	}

	public void setmSurfaceView(GLSurfaceView mSurfaceView) {
		this.mSurfaceView = mSurfaceView;
	}

	public Timer getmTimer() {
		return mTimer;
	}

	public void setmTimer(Timer mTimer) {
		this.mTimer = mTimer;
	}

	public float[] getmVMatrix() {
		return mVMatrix;
	}

	public void setmVMatrix(float[] mVMatrix) {
		this.mVMatrix = mVMatrix;
	}

	public boolean ismEnableDepthBuffer() {
		return mEnableDepthBuffer;
	}

	public void setmEnableDepthBuffer(boolean mEnableDepthBuffer) {
		this.mEnableDepthBuffer = mEnableDepthBuffer;
	}

	public boolean ismClearChildren() {
		return mClearChildren;
	}

	public void setmClearChildren(boolean mClearChildren) {
		this.mClearChildren = mClearChildren;
	}

	public void onSurfaceChanged(GL10 gl, int width, int height) {
	}

	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		GLES20.glFrontFace(GLES20.GL_CCW);
		GLES20.glCullFace(GLES20.GL_BACK);
	}

	public void startRendering() {
		stopRendering();
		mTimer = new Timer();
		mTimer.schedule(new RequestRenderTask(), 0, 1000/mFrameRate);
	}

	public void stopRendering() {
		if(mTimer != null) {
			mTimer.cancel();
			mTimer.purge();
		}
	}

	public void onVisibilityChanged(boolean visible) {
		if(!visible) {
			stopRendering();
		}
		else startRendering();
	}

	public void onSurfaceDestroyed() {
		stopRendering();
	}

	public void setSharedPreferences(SharedPreferences preferences)
	{
		this.preferences = preferences;
	}

	private class RequestRenderTask extends TimerTask {
		public void run() {
			if(mSurfaceView != null)
				mSurfaceView.requestRender();
		}
	}

	public int getFrameRate() {
		return mFrameRate;
	}

	public void setFrameRate(int frameRate) {
		this.mFrameRate = frameRate;
	}

	public GLSurfaceView getSurfaceView() {
		return mSurfaceView;
	}

	public void setSurfaceView(GLSurfaceView surfaceView) {
		this.mSurfaceView = surfaceView;
	}

	public Context getContext() {
		return mContext;
	}

	public int getViewportWidth() {
		return mViewportWidth;
	}

	public int getViewportHeight() {
		return mViewportHeight;
	}
}	
